<!DOCTYPE html>

































































<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" />
<title>Processes and Threads | Android Developers</title>
<link href="../../../assets/android-developer-docs-devguide.css" rel="stylesheet" type="text/css" />
<script src="../../../assets/search_autocomplete.js" type="text/javascript"></script>
<script src="../../../assets/jquery-resizable.min.js" type="text/javascript"></script>
<script src="../../../assets/android-developer-docs.js" type="text/javascript"></script>
<script src="../../../assets/prettify.js" type="text/javascript"></script>
<script type="text/javascript">
  setToRoot("../../../");
</script>
<noscript>
  <style type="text/css">
    html,body{overflow:auto;}
    #body-content{position:relative; top:0;}
    #doc-content{overflow:visible;border-left:3px solid #666;}
    #side-nav{padding:0;}
    #side-nav .toggle-list ul {display:block;}
    #resize-packages-nav{border-bottom:3px solid #666;}
  </style>
</noscript>
</head>
<body class="gc-documentation">

  <div id="header">
      <div id="headerLeft">
          <a href="../../../index.html" tabindex="-1"><img
              src="../../../assets/images/bg_logo.png" alt="Android Developers" /></a>
          <ul id="header-tabs" class="guide">
    
	<li id="home-link"><a href="../../../offline.html">
	
		<span class="en">Home</span>
		<span style="display:none" class="de">Startseite</span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ホーム</span>
		<span style="display:none" class="zh-CN">主页</span>
		<span style="display:none" class="zh-TW">首頁</span>
	
	</a></li>
	<li id="sdk-link"><a href="../../../sdk/index.html">
		<span class="en">SDK</span>
	</a></li>
	<li id="guide-link"><a href="../../../guide/index.html" onClick="return loadLast('guide')">
	
		<span class="en">Dev Guide</span>
		<span style="display:none" class="de">Handbuch</span>
		<span style="display:none" class="es">Guía</span>
		<span style="display:none" class="fr">Guide</span>
		<span style="display:none" class="it">Guida</span>
		<span style="display:none" class="ja">開発ガイド</span>
		<span style="display:none" class="zh-CN">开发人员指南</span>
		<span style="display:none" class="zh-TW">開發指南</span>
	
	</a></li>
	<li id="reference-link"><a href="../../../reference/packages.html" onClick="return loadLast('reference')">
	
		<span class="en">Reference</span>
		<span style="display:none" class="de">Referenz</span>
		<span style="display:none" class="es">Referencia</span>
		<span style="display:none" class="fr">Référence</span>
		<span style="display:none" class="it">Riferimento</span>
		<span style="display:none" class="ja">リファレンス</span>
		<span style="display:none" class="zh-CN">参考</span>
		<span style="display:none" class="zh-TW">參考資料</span>
	
	</a></li>
	<li id="resources-link"><a href="../../../resources/index.html" onClick="return loadLast('resources')">
	
		<span class="en">Resources</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
    		<span style="display:none" class="ja"></span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li id="videos-link"><a href="../../../videos/index.html" onClick="return loadLast('videos')">
	
		<span class="en">Videos</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ビデオ</span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li><a href="http://android-developers.blogspot.com" onClick="return requestAppendHL(this.href)">
	
		<span class="en">Blog</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ブログ</span>
		<span style="display:none" class="zh-CN">博客</span>
		<span style="display:none" class="zh-TW">網誌</span>
	
	</a></li>


     
</ul>
     
      </div>
      <div id="headerRight">
          <div id="headerLinks">
          
          <a href="http://www.android.com">Android.com</a>
          </div>
  <div id="search" >
      <div id="searchForm">
          <form accept-charset="utf-8" class="gsc-search-box" 
                onsubmit="return submit_search()">
            <table class="gsc-search-box" cellpadding="0" cellspacing="0"><tbody>
                <tr>
                  <td class="gsc-input">
                    <input id="search_autocomplete" class="gsc-input" type="text" size="33" autocomplete="off"
                      title="search developer docs" name="q"
                      value="search developer docs"
                      onFocus="search_focus_changed(this, true)"
                      onBlur="search_focus_changed(this, false)"
                      onkeydown="return search_changed(event, true, '../../../')"
                      onkeyup="return search_changed(event, false, '../../../')" />
                  <div id="search_filtered_div" class="no-display">
                      <table id="search_filtered" cellspacing=0>
                      </table>
                  </div>
                  </td>
                  <td class="gsc-search-button">
                    <input type="submit" value="Search" title="search" id="search-button" class="gsc-search-button" />
                  </td>
                  <td class="gsc-clear-button">
                    <div title="clear results" class="gsc-clear-button">&nbsp;</div>
                  </td>
                </tr></tbody>
              </table>
          </form>
      </div><!-- searchForm -->
  </div><!-- search -->
      </div><!-- headerRight -->
      <script type="text/javascript">
        <!--  
        changeTabLang(getLangPref());
        //-->
      </script>
  </div><!-- header -->

  <div class="g-section g-tpl-240" id="body-content">
    <div class="g-unit g-first" id="side-nav">
      <div id="devdoc-nav">
<ul>

  <li>
    <h2><span class="en">Android Basics</span>
        <span class="de" style="display:none">Einführung in Android</span>
        <span class="es" style="display:none">Información básica sobre Android</span>
        <span class="fr" style="display:none">Présentation d'Android</span>
        <span class="it" style="display:none">Nozioni di base su Android</span>
        <span class="ja" style="display:none">Android の基本</span>
        <span class="zh-CN" style="display:none">Android 基础知识</span>
        <span class="zh-TW" style="display:none">Android 簡介</span>
    </h2>
    <ul>
      <li><a href="../../../guide/basics/what-is-android.html">
        <span class="en">What Is Android?</span>
        <span class="de" style="display:none">Was ist Android?</span>
        <span class="es" style="display:none">¿Qué es Android?</span>
        <span class="fr" style="display:none">Qu'est-ce qu'Android&nbsp;?</span>
        <span class="it" style="display:none">Che cos'è Android?</span>
        <span class="ja" style="display:none">Android とは</span>
        <span class="zh-CN" style="display:none">Android 是什么？</span>
        <span class="zh-TW" style="display:none">什麼是 Android？</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals.html">
        <span class="en">Application Fundamentals</span>
        <span class="de" style="display:none">Anwendungsgrundlagen</span>
        <span class="es" style="display:none">Fundamentos de las aplicaciones</span>
        <span class="fr" style="display:none">Principes de base des applications</span>
        <span class="it" style="display:none">Concetti fondamentali sulle applicazioni</span>
        <span class="ja" style="display:none">開発の基礎</span>
        <span class="zh-CN" style="display:none">应用程序基础</span>
        <span class="zh-TW" style="display:none">應用程式基本原理</span>
      </a></li>

  <!--  <li><a style="color:gray;">The Android SDK</a></li> -->
  <!--  <li><a style="color:gray;">Walkthrough for Developers</a></li> -->
      <!-- quick overview of what it's like to develop on Android -->
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Framework Topics</span>
      <span class="de" style="display:none">Framework-Themen</span>
      <span class="es" style="display:none">Temas sobre el framework</span>
      <span class="fr" style="display:none">Thèmes relatifs au framework</span>
      <span class="it" style="display:none">Argomenti relativi al framework</span>
      <span class="ja" style="display:none">フレームワーク トピック</span>
      <span class="zh-CN" style="display:none">框架主题</span>
      <span class="zh-TW" style="display:none">架構主題</span>
    </h2>
    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/activities.html">
          <span class="en">Activities</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/fragments.html">
            <span class="en">Fragments</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/loaders.html">
            <span class="en">Loaders</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/tasks-and-back-stack.html">
            <span class="en">Tasks and Back Stack</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/services.html">
          <span class="en">Services</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/bound-services.html">
            <span class="en">Bound Services</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/topics/providers/content-providers.html">
            <span class="en">Content Providers</span>
          </a></li>
      <li><a href="../../../guide/topics/intents/intents-filters.html">
            <span class="en">Intents and Intent Filters</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals/processes-and-threads.html">
            <span class="en">Processes and Threads</span>
          </a></li>
    </ul>


    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/ui/index.html">
            <span class="en">User Interface</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/ui/declaring-layout.html">
               <span class="en">XML Layouts</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/ui-events.html">
                <span class="en">Input Events</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/menus.html">
               <span class="en">Menus</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/actionbar.html">
               <span class="en">Action Bar</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/dialogs.html">
                <span class="en">Dialogs</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/ui/notifiers/index.html">
                <span class="en">Notifications</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/ui/notifiers/toasts.html">
                <span class="en">Toast Notifications</span>
              </a></li>
              <li><a href="../../../guide/topics/ui/notifiers/notifications.html">
                <span class="en">Status Bar Notifications</span>
              </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/topics/ui/drag-drop.html">
                <span class="en">Drag and Drop</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/themes.html">
                <span class="en">Styles and Themes</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/custom-components.html">
                <span class="en">Custom Components</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/binding.html">
                <span class="en">Binding to Data with AdapterView</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/layout-objects.html">
                <span class="en">Common Layout Objects</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/how-android-draws.html">
                <span class="en">How Android Draws Views</span>
              </a></li>
        </ul>
      </li><!-- end of User Interface -->

      <li class="toggle-list">
        <div><a href="../../../guide/topics/resources/index.html">
               <span class="en">Application Resources</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/resources/providing-resources.html">
                <span class="en">Providing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/accessing-resources.html">
                <span class="en">Accessing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/runtime-changes.html">
                <span class="en">Handling Runtime Changes</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/localization.html">
                <span class="en">Localization</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/resources/available-resources.html">
              <span class="en">Resource Types</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/resources/animation-resource.html">Animation</a></li>
              <li><a href="../../../guide/topics/resources/color-list-resource.html">Color State List</a></li>
              <li><a href="../../../guide/topics/resources/drawable-resource.html">Drawable</a></li>
              <li><a href="../../../guide/topics/resources/layout-resource.html">Layout</a></li>
              <li><a href="../../../guide/topics/resources/menu-resource.html">Menu</a></li>
              <li><a href="../../../guide/topics/resources/string-resource.html">String</a></li>
              <li><a href="../../../guide/topics/resources/style-resource.html">Style</a></li>
              <li><a href="../../../guide/topics/resources/more-resources.html">More Types</a></li>
            </ul>
          </li><!-- end of resource types -->
        </ul>
      </li><!-- end of app resources -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/data/data-storage.html">
            <span class="en">Data Storage</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/data/backup.html">
                <span class="en">Data Backup</span>
              </a>
            </li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/security/security.html">
            <span class="en">Security and Permissions</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/manifest/manifest-intro.html">
          <span class="en">The AndroidManifest.xml File</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/manifest/action-element.html">&lt;action&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-alias-element.html">&lt;activity-alias&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/application-element.html">&lt;application&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/category-element.html">&lt;category&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/compatible-screens-element.html">&lt;compatible-screens&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/data-element.html">&lt;data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/grant-uri-permission-element.html">&lt;grant-uri-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/instrumentation-element.html">&lt;instrumentation&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/intent-filter-element.html">&lt;intent-filter&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/manifest-element.html">&lt;manifest&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/meta-data-element.html">&lt;meta-data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/path-permission-element.html">&lt;path-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-element.html">&lt;permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-group-element.html">&lt;permission-group&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-tree-element.html">&lt;permission-tree&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/provider-element.html">&lt;provider&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/receiver-element.html">&lt;receiver&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/service-element.html">&lt;service&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-gl-texture-element.html">&lt;supports-gl-texture&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-screens-element.html">&lt;supports-screens&gt;</a></li><!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-configuration-element.html">&lt;uses-configuration&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-feature-element.html">&lt;uses-feature&gt;</a></li> <!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-library-element.html">&lt;uses-library&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-permission-element.html">&lt;uses-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a></li>
        </ul>
      </li><!-- end of the manifest file -->
    </ul>

    <ul>
    <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/index.html">
            <span class="en">Graphics</span>
          </a><span class="new-child">new!</span></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/2d-graphics.html">
              <span class="en">Canvas and Drawables</span></a></li>
          <li><a href="../../../guide/topics/graphics/hardware-accel.html">
              <span class="en">Hardware Acceleration</span></a>
            <span class="new">new!</span></li>
          <li><a href="../../../guide/topics/graphics/opengl.html">
              <span class="en">OpenGL</span>
            </a><span class="new">updated</span></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/animation.html">
            <span class="en">Animation</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/prop-animation.html">
              <span class="en">Property Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/view-animation.html">
              <span class="en">View Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/drawable-animation.html">
              <span class="en">Drawable Animation</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
	        <div><a href="../../../guide/topics/renderscript/index.html">
	            <span class="en">RenderScript</span>
	          </a></div>
	        <ul>
	          <li><a href="../../../guide/topics/renderscript/graphics.html">
	                <span class="en">Graphics</span>
	              </a>
	          </li>
	          <li><a href="../../../guide/topics/renderscript/compute.html">
	                <span class="en">Compute</span>
	              </a>
	          </li>
	        </ul>
  	  </li>

      <li class="toggle-list">
          <div><a href="../../../guide/topics/media/index.html">
            <span class="en">Multimedia and Camera</span>
          </a><span class="new">updated</span></div>
          <ul>
            <li><a href="../../../guide/topics/media/mediaplayer.html">
                  <span class="en">Media Playback</span></a>
                </li>
            <li><a href="../../../guide/topics/media/jetplayer.html">
                  <span class="en">JetPlayer</span></a>
                </li>
            <li><a href="../../../guide/topics/media/camera.html">
                  <span class="en">Camera</span></a>
                  <span class="new">new!</span>
                </li>
            <li><a href="../../../guide/topics/media/audio-capture.html">
                  <span class="en">Audio Capture</span></a>
                </li>
          </ul>
      </li>
      <li>
        <a href="../../../guide/topics/clipboard/copy-paste.html">
            <span class="en">Copy and Paste</span>
        </a></li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Sensors</a></div>
          <ul>
            <li><a style="color:gray;">Camera</a></li>
            <li><a style="color:gray;">Compass</a></li>
            <li><a style="color:gray;">Accelerometer</a></li>
          </ul>
      </li> -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/location/index.html">
               <span class="en">Location and Maps</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/location/obtaining-user-location.html">
                <span class="en">Obtaining User Location</span>
              </a></li>
        </ul>
      </li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Wireless Controls</a></div>
          <ul>
            <li><a style="color:gray;">Wi-Fi</a></li>
          </ul>
      </li> -->
  <!--<li><a style="color:gray;">Localization</a></li>  -->
      <li><a href="../../../guide/topics/appwidgets/index.html">
            <span class="en">App Widgets</span></a>
          </li>
      <li><a href="../../../guide/topics/wireless/bluetooth.html">
            <span class="en">Bluetooth</span></a>
          </li>
      <li><a href="../../../guide/topics/nfc/index.html">
            <span class="en">Near Field Communication</span>
          </a></li>
      <li class="toggle-list">
          <div><a href="../../../guide/topics/usb/index.html">
            <span class="en">USB</span></a>
          </div>
            <ul>
              <li><a href="../../../guide/topics/usb/accessory.html">Accessory</a></li>
              <li><a href="../../../guide/topics/usb/host.html">Host</a></li>
              <li><a href="../../../guide/topics/usb/adk.html">Open Accessory Dev Kit</a></li>
            </ul>
          </li>

       <li><a href="../../../guide/topics/network/sip.html">
            <span class="en">Session Initiation Protocol</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/search/index.html">
            <span class="en">Search</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/search/search-dialog.html">Creating a Search Interface</a></li>
            <li><a href="../../../guide/topics/search/adding-recent-query-suggestions.html">Adding Recent Query Suggestions</a></li>
            <li><a href="../../../guide/topics/search/adding-custom-suggestions.html">Adding Custom Suggestions</a></li>
            <li><a href="../../../guide/topics/search/searchable-config.html">Searchable Configuration</a></li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/admin/device-admin.html">
            <span class="en">Device Administration</span></a>
      </li>
      <li class="toggle-list">
           <div>
                <a href="../../../guide/topics/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/topics/testing/testing_android.html">
                <span class="en">Testing Fundamentals</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/activity_testing.html">
                <span class="en">Activity Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/contentprovider_testing.html">
                <span class="en">Content Provider Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/service_testing.html">
                <span class="en">Service Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/what_to_test.html">
                <span class="en">What To Test</span></a>
              </li>
           </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Android Market Topics</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/licensing.html">
          <span class="en">Application Licensing</span></a>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/market/billing/index.html">
            <span class="en">In-app Billing</span></a>
        </div>
        <ul>
          <li><a href="../../../guide/market/billing/billing_overview.html">
              <span class="en">In-app Billing Overview</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_integrate.html">
              <span class="en">Implementing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_best_practices.html">
              <span class="en">Security and Design</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_testing.html">
              <span class="en">Testing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_admin.html">
              <span class="en">Administering In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_reference.html">
              <span class="en">In-app Billing Reference</span></a>
          </li>
        </ul>
      </li>
      <li><a href="../../../guide/appendix/market-filters.html">
          <span class="en">Market Filters</span></a>
      </li>
      <li><a href="../../../guide/market/publishing/multiple-apks.html">
          <span class="en">Multiple APK Support</span></a>
      </li>
    </ul>
  </li>


  <li>
    <h2><span class="en">Developing</span>
               <span class="de" style="display:none">Entwicklung</span>
               <span class="es" style="display:none">Desarrollo</span>
               <span class="fr" style="display:none">Développement</span>
               <span class="it" style="display:none">Sviluppo</span>
               <span class="ja" style="display:none">開発</span>
               <span class="zh-CN" style="display:none">开发</span>
               <span class="zh-TW" style="display:none">開發</span>
    </h2>
    <ul>
  <!--<li><a href="">Developing for Android</a></li>
      signing, upgrading, selecting a package name, select device profile, touch, trackball, dpad available, etc. -->
      <li>
        <a href="../../../guide/developing/index.html">
        <span class="en">Introduction</span></a>
      </li>

      <li class="toggle-list">
        <div>
           <a href="../../../guide/developing/devices/index.html">
                <span class="en">Managing Virtual Devices</span>
            </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/devices/managing-avds.html">
              <span class="en">With AVD Manager</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/devices/managing-avds-cmdline.html">
              <span class="en">From the Command Line</span>
            </a>
          </li>
          <li>
           <a href="../../../guide/developing/devices/emulator.html">
                <span class="en">Using the Android Emulator</span>
            </a>
          </li>
        </ul>
      </li>
      <li>
        <a href="../../../guide/developing/device.html">
          <span class="en">Using Hardware Devices</span>
        </a>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/projects/index.html">
            <span class="en">Managing Projects</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/projects/projects-eclipse.html">
              <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/projects/projects-cmdline.html">
                <span class="en">From the Command Line</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/building/index.html">
            <span class="en">Building and Running</span>
          </a>
        </div>
        <ul>
          <li><a href="../../../guide/developing/building/building-eclipse.html">
            <span class="en">From Eclipse with ADT</span>
          </a></li>
          <li><a href="../../../guide/developing/building/building-cmdline.html">
            <span class="en">From the Command Line</span></a></li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/debugging/index.html">
            <span class="en">Debugging</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects.html">
                <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects-cmdline.html">
                <span class="en">From Other IDEs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/ddms.html">
              <span class="en">Using DDMS</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-log.html">
                <span class="en">Reading and Writing Logs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-ui.html">
                <span class="en">Debugging and Profiling UIs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-tracing.html">
                <span class="en">Profiling with Traceview and dmtracedump</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-devtools.html">
                <span class="en">Using the Dev Tools App</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
           <div>
                <a href="../../../guide/developing/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/developing/testing/testing_eclipse.html">
                  <span class="en">From Eclipse with ADT</span>
                </a>
              </li>

              <li>
                <a href="../../../guide/developing/testing/testing_otheride.html">
                  <span class="en">From Other IDEs</span>
                </a>
              </li>
           </ul>
         </li>

         <li class="toggle-list">
        <div><a href="../../../guide/developing/tools/index.html">
            <span class="en">Tools</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/developing/tools/adb.html">adb</a></li>
          <li><a href="../../../guide/developing/tools/adt.html">ADT</a></li>
          <li><a href="../../../guide/developing/tools/android.html">android</a></li>
          <li><a href="../../../guide/developing/tools/bmgr.html">bmgr</a>
          <li><a href="../../../guide/developing/tools/dmtracedump.html">dmtracedump</a></li>
          <li><a href="../../../guide/developing/tools/draw9patch.html">Draw
          9-Patch</a></li>
          <li><a href="../../../guide/developing/tools/emulator.html">Emulator</a></li>
          <li><a href="../../../guide/developing/tools/etc1tool.html">etc1tool</a></li>
          <li><a href="../../../guide/developing/tools/hierarchy-viewer.html">Hierarchy Viewer</a></li>
          <li><a href="../../../guide/developing/tools/hprof-conv.html">hprof-conv</a></li>
          <li><a href="../../../guide/developing/tools/layoutopt.html">layoutopt</a></li>
          <li><a href="../../../guide/developing/tools/logcat.html">logcat</a></li>
          <li><a href="../../../guide/developing/tools/mksdcard.html">mksdcard</a></li>
          <li><a href="../../../guide/developing/tools/monkey.html">Monkey</a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/developing/tools/monkeyrunner_concepts.html">
              <span class="en">monkeyrunner</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/developing/tools/MonkeyDevice.html">
                <span class="en">MonkeyDevice</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyImage.html">
                <span class="en">MonkeyImage</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyRunner.html">
                <span class="en">MonkeyRunner</span>
                </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/developing/tools/proguard.html">ProGuard</a></li>
          <li><a href="../../../guide/developing/tools/adb.html#sqlite">sqlite3</a></li>
          <li><a href="../../../guide/developing/tools/traceview.html">Traceview</a></li>
          <li><a href="../../../guide/developing/tools/zipalign.html">zipalign</a></li>
        </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Publishing</span>
        <span class="de" style="display:none">Veröffentlichung</span>
        <span class="es" style="display:none">Publicación</span>
        <span class="fr" style="display:none">Publication</span>
        <span class="it" style="display:none">Pubblicazione</span>
        <span class="ja" style="display:none">公開</span>
        <span class="zh-CN" style="display:none">发布</span>
        <span class="zh-TW" style="display:none">發佈</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/app-signing.html">
            <span class="en">Signing Your Applications</span>
            <span class="de" style="display:none">Signieren Ihrer Anwendungen</span>
            <span class="es" style="display:none">Firma de aplicaciones</span>
            <span class="fr" style="display:none">Attribution de votre signature <br />à vos applications</span>
            <span class="it" style="display:none">Firma delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションへの署名</span>
            <span class="zh-CN" style="display:none">应用程序签名</span>
            <span class="zh-TW" style="display:none">簽署應用程式</span>
          </a></li>
      <li><a href="../../../guide/publishing/versioning.html">
            <span class="en">Versioning Your Applications</span>
            <span class="de" style="display:none">Versionsverwaltung für Ihre <br />Anwendungen</span>
            <span class="es" style="display:none">Versiones de las aplicaciones</span>
            <span class="fr" style="display:none">Attribution d'une version à vos applications</span>
            <span class="it" style="display:none">Controllo versioni delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションのバージョニング</span>
            <span class="zh-CN" style="display:none">应用程序版本控制</span>
            <span class="zh-TW" style="display:none">應用程式版本設定</span>
          </a></li>
      <li><a href="../../../guide/publishing/preparing.html">
            <span class="en">Preparing to Publish</span>
            <span class="de" style="display:none">Vorbereitung auf die Veröffentlichung</span>
            <span class="es" style="display:none">Publicación de aplicaciones</span>
            <span class="fr" style="display:none">Préparation à la publication</span>
            <span class="it" style="display:none">Preparativi per la pubblicazione</span>
            <span class="ja" style="display:none">公開の準備</span>
            <span class="zh-CN" style="display:none">准备发布</span>
            <span class="zh-TW" style="display:none">準備發佈</span>
          </a></li>
      <li><a href="../../../guide/publishing/publishing.html">
            <span class="en">Publishing on Android Market</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Best Practices</span>
               <span class="de" style="display:none">Bewährte Verfahren</span>
               <span class="es" style="display:none">Prácticas recomendadas</span>
               <span class="fr" style="display:none">Meilleures pratiques</span>
               <span class="it" style="display:none">Best practice</span>
               <span class="ja" style="display:none">ベスト プラクティス</span>
               <span class="zh-CN" style="display:none">最佳实践</span>
               <span class="zh-TW" style="display:none">最佳實務</span>
    </h2>
    <ul>
      <li><a href="../../../guide/practices/compatibility.html">
            <span class="en">Compatibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/screens_support.html">
          <span class="en">Supporting Multiple Screens</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/practices/screens-distribution.html">
            <span class="en">Distributing to Specific Screens</span>
          </a></li>
          <li><a href="../../../guide/practices/screen-compat-mode.html">
            <span class="en">Screen Compatibility Mode</span>
          </a></li>
          <li><a href="../../../guide/practices/screens-support-1.5.html">
            <span class="en">Strategies for Android 1.5</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/optimizing-for-3.0.html">
            <span class="en">Optimizing Apps for Android 3.0</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/ui_guidelines/index.html">
               <span class="en">UI Guidelines</span>
             </a></div>
        <ul>
          <li class="toggle-list">
            <div><a href="../../../guide/practices/ui_guidelines/icon_design.html">
                   <span class="en">Icon Design <span class="new">updated</span></span>
                 </a></div>
            <ul>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_launcher.html">
                    <span class="en">Launcher Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_menu.html">
                    <span class="en">Menu Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_action_bar.html">
                    <span class="en">Action Bar Icons <span class="new">new!</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_status_bar.html">
                    <span class="en">Status Bar Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_tab.html">
                    <span class="en">Tab Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_dialog.html">
                    <span class="en">Dialog Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_list.html">
                    <span class="en">List View Icons</span>
                  </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/practices/ui_guidelines/widget_design.html">
                <span class="en">App Widget Design <span class="new">updated</span></span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/activity_task_design.html">
                <span class="en">Activity and Task Design</span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/menu_design.html">
                <span class="en">Menu Design</span>
              </a></li>
        </ul>
      </li>
      </ul>
      <ul>
      <li><a href="../../../guide/practices/design/accessibility.html">
            <span class="en">Designing for Accessibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/design/performance.html">
            <span class="en">Designing for Performance</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/practices/design/jni.html">
                <span class="en">JNI Tips</span>
              </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/design/responsiveness.html">
            <span class="en">Designing for Responsiveness</span>
          </a></li>
      <li><a href="../../../guide/practices/design/seamlessness.html">
            <span class="en">Designing for Seamlessness</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Web Applications</span>
    </h2>
    <ul>
      <li><a href="../../../guide/webapps/index.html">
            <span class="en">Web Apps Overview</span>
          </a></li>
      <li><a href="../../../guide/webapps/targeting.html">
            <span class="en">Targeting Screens from Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/webview.html">
            <span class="en">Building Web Apps in WebView</span>
          </a></li>
      <li><a href="../../../guide/webapps/debugging.html">
            <span class="en">Debugging Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/best-practices.html">
            <span class="en">Best Practices for Web Apps</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Appendix</span>
               <span class="de" style="display:none">Anhang</span>
               <span class="es" style="display:none">Apéndice</span>
               <span class="fr" style="display:none">Annexes</span>
               <span class="it" style="display:none">Appendice</span>
               <span class="ja" style="display:none">付録</span>
               <span class="zh-CN" style="display:none">附录</span>
               <span class="zh-TW" style="display:none">附錄</span>
    </h2>
    <ul>
      <li><a href="../../../guide/appendix/api-levels.html">
            <span class="en">Android API Levels</span>
          </a></li>
      <li><a href="../../../guide/appendix/install-location.html">
            <span class="en">App Install Location</span>
          </a></li>
      <li><a href="../../../guide/appendix/media-formats.html">
            <span class="en">Supported Media Formats <span class="new">updated</span></span>
          </a></li>
      <li><a href="../../../guide/appendix/g-app-intents.html">
            <span class="en">Intents List: Google Apps</span>
          </a></li>
      <li><a href="../../../guide/developing/tools/aidl.html">AIDL</a></li>
      <li><a href="../../../guide/appendix/glossary.html">
            <span class="en">Glossary</span>
          </a></li>
    </ul>
  </li>

</ul>

<script type="text/javascript">
<!--
    buildToggleLists();
    changeNavLang(getLangPref());
//-->
</script>

      </div>
    </div> <!-- end side-nav -->
    <script>
      addLoadEvent(function() {
        scrollIntoView("devdoc-nav");
        });
    </script>




<div class="g-unit" id="doc-content"><a name="top"></a>

<div id="jd-header" class="guide-header">
  <span class="crumb">
    &nbsp;
    
  </span>
<h1>Processes and Threads 进程与线程</h1>
</div>

  <div id="jd-content">

    <div class="jd-descr">
    <div id="qv-wrapper">
<div id="qv">
<h2>Quickview</h2>
<ul>
  <li>Every application runs in its own process and all components of the application run in that
process, by default 默认情况下，所有的程序都运行在自己的进程中，并且所有的程序组件也运行在其中。。</li>
  <li>Any slow, blocking operations in an activity should be done in a new thread, to avoid slowing
down the user interface activity中任何运行缓慢，阻塞的操作都应该运行在新的线程中，已避免影响用户体验。</li>
</ul>

<h2>In this document</h2>
<ol>
<li><a href="#Processes">Processes</a>
  <ol>
    <li><a href="#Lifecycle">Process lifecycle</a></li>
  </ol>
</li>
<li><a href="#Threads">Threads</a>
  <ol>
    <li><a href="#WorkerThreads">Worker threads</a></li>
    <li><a href="#ThreadSafe">Thread-safe methods</a></li>
  </ol>
</li>
<li><a href="#IPC">Interprocess Communication</a></li>
</ol>

</div>
</div>

<p>When an application component starts and the application does not have any other components
running, the Android system starts a new Linux process for the application with a single thread of
execution. By default, all components of the same application run in the same process and thread
(called the "main" thread). If an application component starts and there already exists a process
for that application (because another component from the application exists), then the component is
started within that process and uses the same thread of execution. However, you can arrange for
different components in your application to run in separate processes, and you can create additional
threads for any process.</p>
<p>如果某个应用程序组件是第一次被启动，且这时应用程序也没有其他组件在运行，则Android系统会为应用程序创建一个包含单个线程的linux进程。默认情况下，同一个应用程序的所有组件都运行在同一个进程和线程里（叫做“main”主线程）。如果组件启动时，已经存在应用程序的进程了（因为应用程序的其它组件已经在运行了），则此组件会在已有的进程和线程中启动运行。不过，可以指定组件运行在其他进程里，也可以为任何进程创建额外的线程。</p>

<p>This document discusses how processes and threads work in an Android application.</p>
<p>本文讨论进程和线程是如何在Android应用程序中发挥作用的。</p>


<h2 id="Processes">Processes 进程</h2>

<p>By default, all components of the same application run in the same process and most applications
should not change this. However, if you find that you need to control which process a certain
component belongs to, you can do so in the manifest file.</p>
<p>默认情况下，同一个应用程序内的所有组件都是运行在同一个进程中的，大部分应用程序也不会去改变它。不过，如果需要指定某个特定组件所属的进程，则可以利用manifest文件来达到目的。</p>

<p>The manifest entry for each type of component element&mdash;<a
href="../../../guide/topics/manifest/activity-element.html"><code>&lt;activity&gt;</code></a>, <a href="../../../guide/topics/manifest/service-element.html"><code>&lt;service&gt;</code></a>, <a href="../../../guide/topics/manifest/receiver-element.html"><code>&lt;receiver&gt;</code></a>, and <a href="../../../guide/topics/manifest/provider-element.html"><code>&lt;provider&gt;</code></a>&mdash;supports an <code>android:process</code> attribute that can specify a
process in which that component should run. You can set this attribute so that each component runs
in its own process or so that some components share a process while others do not.  You can also set
<code>android:process</code> so that components of different applications run in the same
process&mdash;provided that the applications share the same Linux user ID and are signed with the
same certificates.</p>
<p>manifest文件中的每种组件元素——<code>&lt;activity&gt;</code>、 <code>&lt;service&gt;</code>、 <code>&lt;receiver&gt;</code>和<code>&lt;provider&gt;</code>——都支持定义android:process属性，用于指定组件运行的进程。设置此属性即可实现每个组件在各自的进程中运行，或者某几个组件共享一个进程而其它组件运行于独立的进程。设置此属性也可以让不同应用程序的组件运行在同一个进程中——实现多个应用程序共享同一个Linux用户ID、赋予同样的权限。</p>

<p>The <a href="../../../guide/topics/manifest/application-element.html"><code>&lt;application&gt;</code></a> element also supports an <code>android:process</code> attribute, to set a
default value that applies to all components.</p>
<p><code>&lt;application&gt;</code>元素也支持android:process属性，用于指定所有组件的默认进程</p>

<p>Android might decide to shut down a process at some point, when memory is low and required by
other processes that are more immediately serving the user. Application
components running in the process that's killed are consequently destroyed.  A process is started
again for those components when there's again work for them to do.</p>
<p>如果内存不足，可又有其它为用户提供更紧急服务的进程需要更多内存，Android可能会决定关闭一个进程。在此进程中运行着的应用程序组件也会因此被销毁。当需要再次工作时，会为这些组件重新创建一个进程。</p>

<p>When deciding which processes to kill, the Android system weighs their relative importance to
the user.  For example, it more readily shuts down a process hosting activities that are no longer
visible on screen, compared to a process hosting visible activities. The decision whether to
terminate a process, therefore, depends on the state of the components running in that process. The
rules used to decide which processes to terminate is discussed below. </p>
<p>在决定关闭哪个进程的时候，Android系统会权衡它们相对用户的重要程度。比如，相对于一个拥有可见activity的进程，更有可能去关闭一个activity已经在屏幕上看不见的进程。也就是说，是否终止一个进程，取决于运行在此进程中组件的状态。终止进程的判定规则将在后续内容中讨论。</p>


<h3 id="Lifecycle">Process lifecycle 进程生命周期</h3>

<p>The Android system tries to maintain an application process for as long as possible, but
eventually needs to remove old processes to reclaim memory for new or more important processes.  To
determine which processes to keep
and which to kill, the system places each process into an "importance hierarchy" based on the
components running in the process and the state of those components.  Processes with the lowest
importance are eliminated first, then those with the next lowest importance, and so on, as necessary
to recover system resources.</p>
<p>Android系统试图尽可能长时间地保持应用程序进程，但为了新建或者运行更加重要的进程，总是需要清除过时进程来回收内存。为了决定保留或终止哪个进程，根据进程内运行的组件及这些组件的状态，系统把每个进程都划入一个“重要性层次结构”中。重要性最低的进程首先会被清除，然后是下一个最低的，依此类推，这都是回收系统资源所必需的。</p>

<p>There are five levels in the importance hierarchy. The following list presents the different
types of processes in order of importance (the first process is <em>most important</em> and is
<em>killed last</em>):</p>
<p>重要性层次结构共有5级，以下列表按照重要程度列出了各类进程（第一类进程是最重要的，将最后一个被终止）：</p>

<ol>
  <li><b>Foreground process 前台进程</b>
    <p>A process that is required for what the user is currently doing.  A
      process is considered to be in the foreground if any of the following conditions are true:</p>
	  <p>用户当前操作所必须的进程。满足以下任一条件时，进程被视作处于前台：</p>

      <ul>
        <li>It hosts an <code><a href="../../../reference/android/app/Activity.html">Activity</a></code> that the user is interacting with (the <code><a href="../../../reference/android/app/Activity.html">Activity</a></code>'s <code><a href="../../../reference/android/app/Activity.html#onResume()">onResume()</a></code> method has been
called).<p>其中运行着正与用户交互的Activity（Activity对象的 onResume() 方法已被调用）。</p></li>

        <li>It hosts a <code><a href="../../../reference/android/app/Service.html">Service</a></code> that's bound to the activity that the user is
interacting with.
<p> 其中运行着被正与用户交互的activity绑定的服务Service。</p></li>

        <li>It hosts a <code><a href="../../../reference/android/app/Service.html">Service</a></code> that's running "in the foreground"&mdash;the
service has called <code><a href="../../../reference/android/app/Service.html#startForeground(int, android.app.Notification)">startForeground()</a></code>.<p>其中运行着“前台”服务Service——服务以startForeground()方式被调用。</p></li>

        <li>It hosts a <code><a href="../../../reference/android/app/Service.html">Service</a></code> that's executing one of its lifecycle
callbacks (<code><a href="../../../reference/android/app/Service.html#onCreate()">onCreate()</a></code>, <code><a href="../../../reference/android/app/Service.html#onStart(android.content.Intent, int)">onStart()</a></code>, or <code><a href="../../../reference/android/app/Service.html#onDestroy()">onDestroy()</a></code>).<p>其中运行着正在执行生命周期回调方法（onCreate()、onStart()或onDestroy()）的服务Service。
</p></li>

        <li>It hosts a <code><a href="../../../reference/android/content/BroadcastReceiver.html">BroadcastReceiver</a></code> that's executing its <code><a href="../../../reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)">onReceive()</a></code> method.<p>其中运行着正在执行onReceive()方法的BroadcastReceiver。</p></li>
    </ul>

    <p>Generally, only a few foreground processes exist at any given time.  They are killed only as
a last resort&mdash;if memory is so low that they cannot all continue to run.  Generally, at that
point, the device has reached a memory paging state, so killing some foreground processes is
required to keep the user interface responsive.</p>
<p>一般而言，任何时刻前台进程都是为数不多的，只有作为最后的策略——当内存不足以维持它们同时运行时——才会被终止。通常，设备这时候已经到了内存分页状态(memory paging state)的地步，终止一些前台进程是为了保证用户界面的及时响应。</p></li>

  <li><b>Visible process 可见进程</b>
    <p>A process that doesn't have any foreground components, but still can
      affect what the user sees on screen. A process is considered to be visible if either of the
      following conditions are true:</p>
	  <p>没有前台组件、但仍会影响用户在屏幕上所见内容的进程。满足以下任一条件时，进程被认为是可见的：</p>

      <ul>
        <li>It hosts an <code><a href="../../../reference/android/app/Activity.html">Activity</a></code> that is not in the foreground, but is still
visible to the user (its <code><a href="../../../reference/android/app/Activity.html#onPause()">onPause()</a></code> method has been called). 
This might occur, for example, if the foreground activity started a dialog, which allows the
previous activity to be seen behind it.
<p> 其中运行着不在前台的Activity，但用户仍然可见到此activity（onPause()方法被调用了）。比如以下场合就可能发生这种情况：前台activity打开了一个对话框，而之前的activity还允许显示在后面。</p></li>

        <li>It hosts a <code><a href="../../../reference/android/app/Service.html">Service</a></code> that's bound to a visible (or foreground)
activity.
<p>其中运行着被可见（或前台）activity绑定的服务Service。</p></li>
      </ul>

      <p>A visible process is considered extremely important and will not be killed unless doing so
is required to keep all foreground processes running. </p>
<p>可见进程被认为是非常重要的进程，除非无法维持所有前台进程同时运行了，它们是不会被终止的。</p>
    </li>

  <li><b>Service process 服务进程</b>
    <p>A process that is running a service that has been started with the <code><a href="../../../reference/android/content/Context.html#startService(android.content.Intent)">startService()</a></code> method and does not fall into either of the two
higher categories. Although service processes are not directly tied to anything the user sees, they
are generally doing things that the user cares about (such as playing music in the background or
downloading  data on the network), so the system keeps them running unless there's not enough memory
to retain them along with all foreground and visible processes. </p>
<p>此进程运行着由startService()方法启动的服务，它不会升级为上述两级别。尽管服务进程不直接和用户所见内容关联，但他们通常在执行一些用户关心的操作（比如在后台播放音乐或从网络下载数据）。因此，除非内存不足以维持所有前台、可见进程同时运行，系统会保持服务进程的运行。</p>
  </li>

  <li><b>Background process 后台进程</b>
    <p>A process holding an activity that's not currently visible to the user  (the activity's
<code><a href="../../../reference/android/app/Activity.html#onStop()">onStop()</a></code> method has been called). These processes have no direct
impact on the user experience, and the system can kill them at any time to reclaim memory for a
foreground,
visible, or service process. Usually there are many background processes running, so they are kept
in an LRU (least recently used) list to ensure that the process with the activity that was most
recently seen by the user is the last to be killed. If an activity implements its lifecycle methods
correctly, and saves its current state, killing its process will not have a visible effect on
the user experience, because when the user navigates back to the activity, the activity restores
all of its visible state. See the <a
href="../../../guide/topics/fundamentals/activities.html#SavingActivityState">Activities</a>
document for information about saving and restoring state.</p>
<p>包含目前用户不可见activity（Activity对象的onStop()方法已被调用）的进程。这些进程对用户体验没有直接的影响，系统可能在任意时间终止它们，以回收内存供前台进程、可见进程及服务进程使用。通常会有很多后台进程在运行，所以它们被保存在一个LRU（最近最少使用）列表中，以确保最近被用户使用的activity最后一个被终止。如果一个activity正确实现了生命周期方法，并保存了当前的状态，则终止此类进程不会对用户体验产生可见的影响。因为在用户返回时，activity会恢复所有可见的状态。关于保存和恢复状态的详细信息，请参阅Activities文档。</p>
  </li>

  <li><b>Empty process 空进程</b>
    <p>A process that doesn't hold any active application components.  The only reason to keep this
kind of process alive is for caching purposes, to improve startup time the next time a component
needs to run in it.  The system often kills these processes in order to balance overall system
resources between process caches and the underlying kernel caches.</p>
<p>不含任何活动应用程序组件的进程。保留这种进程的唯一目的就是用作缓存，以改善下次在此进程中运行组件的启动时间。为了在进程缓存和内核缓存间平衡系统整体资源，系统经常会终止这种进程。</p>
  </li>
</ol>


  <p>Android ranks a process at the highest level it can, based upon the importance of the
components currently active in the process.  For example, if a process hosts a service and a visible
activity, the process is ranked as a visible process, not a service process.</p>
<p>依据进程中目前活跃组件的重要程度，Android会给进程评估一个尽可能高的级别。例如：如果一个进程中运行着一个服务和一个用户可见的activity，则此进程会被评定为可见进程，而不是服务进程。</p>

  <p>In addition, a process's ranking might be increased because other processes are dependent on
it&mdash;a process that is serving another process can never be ranked lower than the process it is
serving. For example, if a content provider in process A is serving a client in process B, or if a
service in process A is bound to a component in process B, process A is always considered at least
as important as process B.</p>
<p>此外，一个进程的级别可能会由于其它进程的依赖而被提高——为其它进程提供服务的进程级别永远不会低于使用此服务的进程。比如：如果A进程中的content provider为进程B中的客户端提供服务，或进程A中的服务被进程B中的组件所调用，则A进程至少被视为与进程B同样重要。</p>

  <p>Because a process running a service is ranked higher than a process with background activities,
an activity that initiates a long-running operation might do well to start a <a
href="../../../guide/topics/fundamentals/services.html">service</a> for that operation, rather than
simply create a worker thread&mdash;particularly if the operation will likely outlast the activity.
For example, an activity that's uploading a picture to a web site should start a service to perform
the upload so that the upload can continue in the background even if the user leaves the activity.
Using a service guarantees that the operation will have at least "service process" priority,
regardless of what happens to the activity. This is the same reason that broadcast receivers should
employ services rather than simply put time-consuming operations in a thread.</p>
<p>因为运行服务的进程级别是高于后台activity进程的，所以，如果activity需要启动一个长时间运行的操作，则为其启动一个服务service会比简单地创建一个工作线程更好些——尤其是在此操作时间比activity本身存在时间还要长久的情况下。比如，一个activity要把图片上传至Web网站，就应该创建一个服务来执行之，即使用户离开了此activity，上传还是会在后台继续运行。不论activity发生什么情况，使用服务可以保证操作至少拥有“服务进程”的优先级。同理，上一篇中的广播接收器broadcast receiver也是使用服务而非线程来处理耗时任务的。</p>



<h2 id="Threads">Threads 线程</h2>

<p>When an application is launched, the system creates a thread of execution for the application,
called "main." This thread is very important because it is in charge of dispatching events to
the appropriate user interface widgets, including drawing events. It is also the thread in which
your application interacts with components from the Android UI toolkit (components from the <code><a href="../../../reference/android/widget/package-summary.html">android.widget</a></code> and <code><a href="../../../reference/android/view/package-summary.html">android.view</a></code> packages). As such, the main thread is also sometimes
called the UI thread.</p>
<p>应用程序启动时，系统会为它创建一个名为“main”的主线程。主线程非常重要，因为它负责把事件分发给相应的用户界面widget——包括屏幕绘图事件。它也是应用程序与Android UI组件包（来自android.widget和android.view包）进行交互的线程。因此，主线程有时也被叫做UI线程。</p>

<p>The system does <em>not</em> create a separate thread for each instance of a component. All
components that run in the same process are instantiated in the UI thread, and system calls to
each component are dispatched from that thread. Consequently, methods that respond to system
callbacks (such as <code><a href="../../../reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent)">onKeyDown()</a></code> to report user actions
or a lifecycle callback method) always run in the UI thread of the process.</p>
<p>系统并不会为每个组件的实例都创建单独的线程。运行于同一个进程中的所有组件都是在UI线程中实例化的，对每个组件的系统调用也都是由UI线程分发的。因此，对系统回调进行响应的方法（比如报告用户操作的onKeyDown()或生命周期回调方法）总是运行在UI线程中。</p>

<p>For instance, when the user touches a button on the screen, your app's UI thread dispatches the
touch event to the widget, which in turn sets its pressed state and posts an invalidate request to
the event queue. The UI thread dequeues the request and notifies the widget that it should redraw
itself.</p>
<p>举个例子，当用户触摸屏幕上的按钮时，应用程序的UI线程把触摸事件分发给widget，widget先把自己置为按下状态，再发送一个显示区域已失效（invalidate）的请求到事件队列中。UI线程从队列中取出此请求，并通知widget重绘自己。</p>

<p>When your app performs intensive work in response to user interaction, this single thread model
can yield poor performance unless you implement your application properly. Specifically, if
everything is happening in the UI thread, performing long operations such as network access or
database queries will block the whole UI. When the thread is blocked, no events can be dispatched,
including drawing events. From the user's perspective, the
application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds
(about 5 seconds currently) the user is presented with the infamous "<a
href="http://developer.android.com/guide/practices/design/responsiveness.html">application not
responding</a>" (ANR) dialog. The user might then decide to quit your application and uninstall it
if they are unhappy.</p>
<p>如果应用程序在与用户交互的同时需要执行繁重的任务，单线程模式可能会导致运行性能很低下，除非应用程序的执行时机刚好很合适。如果UI线程需要处理每一件事情，那些耗时很长的操作——诸如访问网络或查询数据库等——将会阻塞整个UI（线程）。一旦线程被阻塞，所有事件都不能被分发，包括屏幕绘图事件。从用户的角度看来，应用程序看上去像是挂起了。更糟糕的是，如果UI线程被阻塞超过一定时间（目前大约是5秒钟），用户就会被提示那个可恶的“应用程序没有响应”(ANR)对话框。如果引起用户不满，他可能就会决定退出并删除这个应用程序。</p>

<p>Additionally, the Andoid UI toolkit is <em>not</em> thread-safe. So, you must not manipulate
your UI from a worker thread&mdash;you must do all manipulation to your user interface from the UI
thread. Thus, there are simply two rules to Android's single thread model:</p>
<p>此外，Andoid的UI组件包并不是线程安全的。因此不允许从工作线程中操作UI——只能从UI线程中操作用户界面。于是，Andoid的单线程模式必须遵守两个规则：</p>

<ol>
<li>Do not block the UI thread <p>不要阻塞UI线程。</p>
<li>Do not access the Android UI toolkit from outside the UI thread <p>不要在UI线程之外访问Andoid的UI组件包。</p>
</ol>

<h3 id="WorkerThreads">Worker threads 工作线程</h3>

<p>Because of the single thread model described above, it's vital to the responsiveness of your
application's UI that you do not block the UI thread. If you have operations to perform
that are not instantaneous, you should make sure to do them in separate threads ("background" or
"worker" threads).</p>
<p>根据对以上单线程模式的描述，要想保证程序界面的响应能力，关键是不能阻塞UI线程。如果操作不能很快完成，应该让它们在单独的线程中运行（“后台”或“工作”线程）。</p>

<p>For example, below is some code for a click listener that downloads an image from a separate
thread and displays it in an <code><a href="../../../reference/android/widget/ImageView.html">ImageView</a></code>:</p>
<p>例如：以下响应鼠标点击的代码实现了在单独线程中下载图片并在ImageView显示：</p>

<pre>
public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            Bitmap b = loadImageFromNetwork("http://example.com/image.png");
            mImageView.setImageBitmap(b);
        }
    }).start();
}
</pre>

<p>At first, this seems to work fine, because it creates a new thread to handle the network
operation. However, it violates the second rule of the single-threaded model: <em>do not access the
Android UI toolkit from outside the UI thread</em>&mdash;this sample modifies the <code><a href="../../../reference/android/widget/ImageView.html">ImageView</a></code> from the worker thread instead of the UI thread. This can result in
undefined and unexpected behavior, which can be difficult and time-consuming to track down.</p>
<p>乍看起来，这段代码似乎能运行得很好，因为创建了一个新的线程来处理访问网络的操作。可是它违反了单线程模式的第二条规则：不要在UI线程之外访问Andoid的UI组件包——以上例子在工作线程里而不是UI线程里修改了ImageView。这可能导致不明确、不可预见的后果，要跟踪这种情况也是很困难很耗时间的。</p>

<p>To fix this problem, Android offers several ways to access the UI thread from other
threads. Here is a list of methods that can help:</p>
<p>为了解决以上问题，Android提供了几种途径来从其它线程中访问UI线程。下面列出了有助于解决问题的几种方法：</p>

<ul>
<li><code><a href="../../../reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)">Activity.runOnUiThread(Runnable)</a></code></li>
<li><code><a href="../../../reference/android/view/View.html#post(java.lang.Runnable)">View.post(Runnable)</a></code></li>
<li><code><a href="../../../reference/android/view/View.html#postDelayed(java.lang.Runnable, long)">View.postDelayed(Runnable,
long)</a></code></li>
</ul>

<p>For example, you can fix the above code by using the <code><a href="../../../reference/android/view/View.html#post(java.lang.Runnable)">View.post(Runnable)</a></code> method:</p>
<p>比如说，可以使用View.post(Runnable)方法来修正上面的代码：</p>

<pre>
public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}
</pre>

<p>Now this implementation is thread-safe: the network operation is done from a separate thread
while the <code><a href="../../../reference/android/widget/ImageView.html">ImageView</a></code> is manipulated from the UI thread.</p>
<p>以上代码的执行现在是线程安全的了：网络相关的操作在单独的线程里完成，而ImageView是在UI线程里操纵的。</p>

<p>However, as the complexity of the operation grows, this kind of code can get complicated and
difficult to maintain. To handle more complex interactions with a worker thread, you might consider
using a <code><a href="../../../reference/android/os/Handler.html">Handler</a></code> in your worker thread, to process messages delivered from the UI
thread. Perhaps the best solution, though, is to extend the <code><a href="../../../reference/android/os/AsyncTask.html">AsyncTask</a></code> class,
which simplifies the execution of worker thread tasks that need to interact with the UI.</p>
<p>不过，随着操作变得越来越复杂，这类代码也会变得很复杂很难维护。为了用工作线程完成更加复杂的交互处理，可以考虑在工作线程中用Handler来处理UI线程分发过来的消息。当然，最好的解决方案也许就是继承使用异步任务类AsyncTask，此类简化了一些工作线程和UI交互的操作。</p>


<h4 id="AsyncTask">Using AsyncTask 使用异步任务</h4>

<p><code><a href="../../../reference/android/os/AsyncTask.html">AsyncTask</a></code> allows you to perform asynchronous work on your user
interface. It performs the blocking operations in a worker thread and then publishes the results on
the UI thread, without requiring you to handle threads and/or handlers yourself.</p>
<p>异步任务AsyncTask 允许以异步的方式对用户界面进行操作。它先阻塞工作线程，再在UI线程中呈现结果，在此过程中不需要对线程和handler进行人工干预。</p>

<p>To use it, you must subclass <code><a href="../../../reference/android/os/AsyncTask.html">AsyncTask</a></code> and implement the <code><a href="../../../reference/android/os/AsyncTask.html#doInBackground(Params...)">doInBackground()</a></code> callback method, which runs in a pool of
background threads. To update your UI, you should implement <code><a href="../../../reference/android/os/AsyncTask.html#onPostExecute(Result)">onPostExecute()</a></code>, which delivers the result from <code><a href="../../../reference/android/os/AsyncTask.html#doInBackground(Params...)">doInBackground()</a></code> and runs in the UI thread, so you can safely
update your UI. You can then run the task by calling <code><a href="../../../reference/android/os/AsyncTask.html#execute(Params...)">execute()</a></code>
from the UI thread.</p>
<p>要使用异步任务，必须继承AsyncTask类并实现doInBackground()回调方法，该对象将运行于一个后台线程池中。要更新UI时，须实现onPostExecute()方法来分发doInBackground()返回的结果，由于此方法运行在UI线程中，所以就能安全地更新UI了。然后就可以在UI线程中调用execute()来执行任务了。</p>

<p>For example, you can implement the previous example using <code><a href="../../../reference/android/os/AsyncTask.html">AsyncTask</a></code> this
way:</p>
<p>例如，可以利用AsyncTask来实现上面的那个例子：</p>

<pre>
public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }
    
    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}
</pre>

<p>Now the UI is safe and the code is simpler, because it separates the work into the
part that should be done on a worker thread and the part that should be done on the UI thread.</p>
<p>现在UI是安全的，代码也得到简化，因为任务分解成了工作线程内完成的部分和UI线程内完成的部分。</p>

<p>You should read the <code><a href="../../../reference/android/os/AsyncTask.html">AsyncTask</a></code> reference for a full understanding on
how to use this class, but here is a quick overview of how it works:</p>
<p>要全面理解这个类的使用，须阅读AsyncTask的参考文档。以下是关于其工作方式的概述：</p>

<ul>
<li>You can specify the type of the parameters, the progress values, and the final
value of the task, using generics
<p>可以用generics来指定参数、进度值和任务最终值的类型。</p></li>
<li>The method <code><a href="../../../reference/android/os/AsyncTask.html#doInBackground(Params...)">doInBackground()</a></code> executes automatically
on a worker thread
<p>工作线程中的doInBackground()方法会自动执行。</p></li>
<li><code><a href="../../../reference/android/os/AsyncTask.html#onPreExecute()">onPreExecute()</a></code>, <code><a href="../../../reference/android/os/AsyncTask.html#onPostExecute(Result)">onPostExecute()</a></code>, and <code><a href="../../../reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)">onProgressUpdate()</a></code> are all invoked on the UI thread<p>  onPreExecute()、onPostExecute()和onProgressUpdate()方法都在UI线程中调用。</p></li>
<li>The value returned by <code><a href="../../../reference/android/os/AsyncTask.html#doInBackground(Params...)">doInBackground()</a></code> is sent to
<code><a href="../../../reference/android/os/AsyncTask.html#onPostExecute(Result)">onPostExecute()</a></code>
<p> doInBackground()的返回值会传给onPostExecute()。</p></li>
<li>You can call <code><a href="../../../reference/android/os/AsyncTask.html#publishProgress(Progress...)">publishProgress()</a></code> at anytime in <code><a href="../../../reference/android/os/AsyncTask.html#doInBackground(Params...)">doInBackground()</a></code> to execute <code><a href="../../../reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)">onProgressUpdate()</a></code> on the UI thread
<p>在doInBackground()内的任何时刻，都可以调用publishProgress()来执行UI线程中的onProgressUpdate()。</p></li>
<li>You can cancel the task at any time, from any thread
<p> 可以在任何时刻、任何线程内取消任务。</p></li>
</ul>

<p class="caution"><strong>Caution:</strong> Another problem you might encounter when using a worker
thread is unexpected restarts in your activity due to a <a
href="../../../guide/topics/resources/runtime-changes.html">runtime configuration change</a>
(such as when the user changes the screen orientation), which may destroy your worker thread. To
see how you can persist your task during one of these restarts and how to properly cancel the task
when the activity is destroyed, see the source code for the <a
href="http://code.google.com/p/shelves/">Shelves</a> sample application.</p>
<p class="caution"><strong>Caution:</strong>在使用工作线程时，可能遇到的另一个问题是由于运行配置的改变（比如用户改变了屏幕方向）导致activity意外重启，这可能会销毁该工作线程。要了解如何在这种情况下维持任务执行、以及如何在activity被销毁时正确地取消任务，请参见Shelves例程的源代码。</p>


<h3 id="ThreadSafe">Thread-safe methods 线程安全的方法</h3>

<p> In some situations, the methods you implement might be called from more than one thread, and
therefore must be written to be thread-safe. </p>
<p>在某些场合，方法可能会从不止一个线程中被调用，因此这些方法必须是写成线程安全的。</p>

<p>This is primarily true for methods that can be called remotely&mdash;such as methods in a <a
href="../../../guide/topics/fundamentals/bound-services.html">bound service</a>. When a call on a
method implemented in an <code><a href="../../../reference/android/os/IBinder.html">IBinder</a></code> originates in the same process in which the
<code><a href="../../../reference/android/os/IBinder.html">IBinder</a></code> is running, the method is executed in the caller's thread.
However, when the call originates in another process, the method is executed in a thread chosen from
a pool of threads that the system maintains in the same process as the <code><a href="../../../reference/android/os/IBinder.html">IBinder</a></code> (it's not executed in the UI thread of the process).  For example, whereas a service's
<code><a href="../../../reference/android/app/Service.html#onBind(android.content.Intent)">onBind()</a></code> method would be called from the UI thread of the
service's process, methods implemented in the object that <code><a href="../../../reference/android/app/Service.html#onBind(android.content.Intent)">onBind()</a></code> returns (for example, a subclass that implements RPC methods) would be called from threads
in the pool. Because a service can have more than one client, more than one pool thread can engage
the same <code><a href="../../../reference/android/os/IBinder.html">IBinder</a></code> method at the same time.  <code><a href="../../../reference/android/os/IBinder.html">IBinder</a></code> methods must, therefore, be implemented to be thread-safe.</p>
<p>对于能被远程调用的方法——比如绑定服务（bound service）中的方法，这是理所当然的。如果对IBinder所实现方法的调用发起于IBinder所在进程的内部，那么这个方法是执行在调用者的线程中的。但是，如果调用发起于其他进程，那么这个方法将运行于线程池中选出的某个线程中（而不是运行于进程的UI线程中），该线程池由系统维护且位于IBinder所在的进程中。例如，即使一个服务的onBind()方法是从服务所在进程的UI线程中调用的，实现了onBind()的方法对象（比如，实现了RPC方法的一个子类）仍会从线程池中的线程被调用。因为一个服务可以有不止一个客户端，所以同时可以有多个线程池与同一个IBinder方法相关联。因此IBinder方法必须实现为线程安全的。</p>

<p> Similarly, a content provider can receive data requests that originate in other processes.
Although the <code><a href="../../../reference/android/content/ContentResolver.html">ContentResolver</a></code> and <code><a href="../../../reference/android/content/ContentProvider.html">ContentProvider</a></code>
classes hide the details of how the interprocess communication is managed, <code><a href="../../../reference/android/content/ContentProvider.html">ContentProvider</a></code> methods that respond to those requests&mdash;the methods <code><a href="../../../reference/android/content/ContentProvider.html#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)">query()</a></code>, <code><a href="../../../reference/android/content/ContentProvider.html#insert(android.net.Uri, android.content.ContentValues)">insert()</a></code>, <code><a href="../../../reference/android/content/ContentProvider.html#delete(android.net.Uri, java.lang.String, java.lang.String[])">delete()</a></code>, <code><a href="../../../reference/android/content/ContentProvider.html#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])">update()</a></code>, and <code><a href="../../../reference/android/content/ContentProvider.html#getType(android.net.Uri)">getType()</a></code>&mdash;are called from a pool of threads in the content provider's process, not the UI
thread for the process.  Because these methods might be called from any number of threads at the
same time, they too must be implemented to be thread-safe. </p>
<p>类似地，内容提供者（content provider）也能接收来自其它进程的数据请求。尽管ContentResolver类、ContentProvider类隐藏了进程间通讯管理的细节，ContentProvider中响应请求的方法——query()、insert()、delete()、update()和getType()方法——是从ContentProvider所在进程的线程池中调用的，而不是进程的UI线程。因为这些方法可能会从很多线程同时调用，它们也必须实现为线程安全的。</p>

<h2 id="IPC">Interprocess Communication 进程间通信</h2>

<p>Android offers a mechanism for interprocess communication (IPC) using remote procedure calls
(RPCs), in which a method is called by an activity or other application component, but executed
remotely (in another process), with any result returned back to the
caller. This entails decomposing a method call and its data to a level the operating system can
understand, transmitting it from the local process and address space to the remote process and
address space, then reassembling and reenacting the call there.  Return values are then
transmitted in the opposite direction.  Android provides all the code to perform these IPC
transactions, so you can focus on defining and implementing the RPC programming interface. </p>
<p>Android利用远程过程调用（remote procedure call，RPC）提供了一种进程间通信（IPC）机制，通过这种机制，被activity或其他应用程序组件调用的方法将（在其他进程中）被远程执行，而所有的结果将被返回给调用者。这就要求把方法调用及其数据分解到操作系统可以理解的程度，并将其从本地的进程和地址空间传输至远程的进程和地址空间，然后在远程进程中重新组装并执行这个调用。执行后的返回值将被反向传输回来。Android提供了执行IPC事务所需的全部代码，因此只要把注意力放在定义和实现RPC编程接口上即可。
</p>

<p>To perform IPC, your application must bind to a service, using <code><a href="../../../reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)">bindService()</a></code>. For more information, see the <a
href="../../../guide/topics/fundamentals/services.html">Services</a> developer guide.</p>
<p>要执行IPC，应用程序必须用bindService()绑定到服务上。详情请参阅服务Services开发指南。</p>

<!--
<h2>Beginner's Path</h2>

<p>For information about how to perform work in the background for an indefinite period of time
(without a user interface), continue with the <b><a
href="../../../guide/topics/fundamentals/services.html">Services</a></b> document.</p>
-->

    </div>

  <a href="#top" style="float:right">&uarr; Go to top</a>
  
  </div>

<div id="footer">


  <div id="copyright">
    
  Except as noted, this content is licensed under <a
  href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>. 
  For details and restrictions, see the <a href="../../../license.html">
  Content License</a>.
  </div>
  <div id="build_info">
    
  Android 4.0&nbsp;r1 - 17 Oct 2011 18:17

  </div>

  <div id="footerlinks">
    
  <p>
    <a href="http://www.android.com/terms.html">Site Terms of Service</a> -
    <a href="http://www.android.com/privacy.html">Privacy Policy</a> -
    <a href="http://www.android.com/branding.html">Brand Guidelines</a>
  </p>
  </div>

</div> <!-- end footer -->

</div><!-- end doc-content -->

</div> <!-- end body-content --> 

<script type="text/javascript">
init(); /* initialize android-developer-docs.js */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5831155-1");
pageTracker._trackPageview();
</script>

</body>
</html>



